#!/bin/sh
#
########## CHECK ASUS ROUTER MODEL ##########
#
ROUTER_MODEL=$(nvram get model)
#
########## END ##########
#
#
########## SET THE CUSTOM VARIABLES ##########
#
# 设置自定义变量
# 当该程序运行结束，此处设置的变量自动取消
#
# USB_MOUNT_POINT: The mount point of the USB flash drive
# 设置自定义变量USB_MOUNT_POINT为U盘挂载点
USB_MOUNT_POINT=$(nvram get script_usbmount | /opt/bin/sed 's/\/script_bootloader.*//')
#
# SBL_SYS_ROOT: The root directory for SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER
# 设置自定义变量SBL_SYS_ROOT为SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER系统根目录
SBL_SYS_ROOT=${USB_MOUNT_POINT}/script_bootloader
#
# SBL_SYS_ETC: The directory which contains all the configuration files related to SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER
# 设置自定义变量SBL_SYS_ETC为SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER系统配置文件目录
SBL_SYS_ETC=${SBL_SYS_ROOT}/etc
#
# SBL_SYS_TMP: The directory which contains all the temporary files related to SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER
# 设置自定义变量SBL_SYS_TMP为SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER系统临时文件目录
SBL_SYS_TMP=${SBL_SYS_ROOT}/tmp
#
# SBL_SYS_USR: The directory which contains all the external software resources (add-ons) related to SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER
# 设置自定义变量SBL_SYS_USR为SCRIPTS-BOOTLOADER-FOR-ASUS-ROUTER系统外部软件资源目录，即插件目录
SBL_SYS_USR=${SBL_SYS_ROOT}/usr
#
# NAME OF THE SERVICE
# 服务名称
SERVICE_NAME="ADDONS"
#
# FUNCTION OF THE SERVICE
# 服务功能
SERVICE_FUNCTION="INSTALLATION"
#
########## END ##########
#
#
########## GENERATE ADDON LIST ##########
#
# Check if ${SBL_SYS_ETC}/exclusion_list_of_addons exists
# 检查文件${SBL_SYS_ETC}/exclusion_list_of_addons是否存在
if [ -e "${SBL_SYS_ETC}/exclusion_list_of_addons" ]
then
    /opt/bin/find ${SBL_SYS_USR} -name "*_install" | /opt/bin/grep -vEf ${SBL_SYS_ETC}/exclusion_list_of_addons > ${SBL_SYS_TMP}/addon_install_list
    #
else
    # Exclude system components and special flags
    # 如果文件${SBL_SYS_ETC}/exclusion_list_of_addons不存在，则执行
    /opt/bin/echo -e "\033[1;41;37m***** ERROR: UNABLE TO LOCATE ${SBL_SYS_ETC}/exclusion_list_of_addons *****\033[0m\n\033[1;41;37m***** EXCLUDE SYSTEM COMPONENTS AND SPECIAL FLAGS INSTEAD *****\033[0m\n" | /opt/bin/tee -a ${SBL_SYS_TMP}/addon_installation_log
    #
    /opt/bin/find ${SBL_SYS_USR} -name "*_install" | /opt/bin/grep -vE '(dependency|dnsmasq.d|entware|fwd|monit|overture|software|swap|timezone|dev_|old_|deprecated)' > ${SBL_SYS_TMP}/addon_install_list
fi
#
########## END ##########
#
#
########## ADDONS INSTALLATION ##########
#
while [ 1 ]
do
    # Print the menu for addons
    # 打印插件菜单
    ADDON_COUNT=$(/opt/bin/cat ${SBL_SYS_TMP}/addon_install_list | /opt/bin/wc -l)
    ADDON_ORDER=1
    /opt/bin/echo -e "Choose the addon you want to install:\n"
    /opt/bin/echo -e "\033[1;31m0. exit\033[0m"
    while [ ${ADDON_ORDER} -le ${ADDON_COUNT} ]
    do
        /opt/bin/echo -n "${ADDON_ORDER}. "
        /opt/bin/sed -E -n -e 's/.*\/([[:alnum:]_-]*)_install/\1/' -e ''"${ADDON_ORDER}"'p' ${SBL_SYS_TMP}/addon_install_list
        ADDON_ORDER=$((${ADDON_ORDER} + 1))
    done
    #
    /opt/bin/echo -en "\nInput ONE INTEGER every time: "
    #
    # Read the input of user
    # 读取用户输入值
    read INPUT_VALUE
    #
    # Check if value ${INPUT_VALUE} is valid
    # 检查值${INPUT_VALUE}是否有效
    if [ ${INPUT_VALUE} -lt 0 ] || [ ${INPUT_VALUE} -gt ${ADDON_COUNT} ]
    then
        /opt/bin/echo -e "\033[1;41;37m***** ERROR: INVALID INPUT VALUE *****\033[0m\n"
        #
    else
        if [ ${INPUT_VALUE} -eq 0 ]
        then
            break
        fi
        ADDON_TO_INSTALL=$(/opt/bin/sed -E -n -e 's/.*\/([[:alnum:]_-]*)_install/\1/' -e ''"${INPUT_VALUE}"'p' ${SBL_SYS_TMP}/addon_install_list)
        #
        # Check if ${SBL_SYS_TMP}/${ADDON_TO_INSTALL} exists
        # 检查文件${SBL_SYS_TMP}/${ADDON_TO_INSTALL}是否存在
        if [ ! -e ${SBL_SYS_TMP}/${ADDON_TO_INSTALL} ]
        then
            # Install the addon chosen by user
            # 安装用户选择的插件
            /opt/bin/touch ${SBL_SYS_TMP}/${ADDON_TO_INSTALL}
            $(/opt/bin/sed -ne ''"${INPUT_VALUE}"'p' ${SBL_SYS_TMP}/addon_install_list)
            #
            # Check if return value is 0
            # 检查返回值是否为0
            if [ $? == '0' ]
            then
                /opt/bin/echo -e "\033[1;42;37m***** ${ADDON_TO_INSTALL}: SUCCEEDED *****\033[0m\n" | /opt/bin/tee -a ${SBL_SYS_TMP}/addon_installation_log
                #
            else
                /opt/bin/echo -e "\033[1;41;37m***** ${ADDON_TO_INSTALL}: FAILED *****\033[0m\n" | /opt/bin/tee -a ${SBL_SYS_TMP}/addon_installation_log
            fi
            #
        else
            /opt/bin/echo -e "\033[1;41;37m***** ERROR: ${ADDON_TO_INSTALL} HAS BEEN INSTALLED *****\033[0m\n" | /opt/bin/tee -a ${SBL_SYS_TMP}/addon_installation_log
        fi
    fi
done
#
########## END ##########